package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.BaseArticleBaseDTO;
import com.yuanfeng.base.entity.BaseArticleBaseEntity;
import com.yuanfeng.base.mapper.BaseArticleBaseMapper;
import com.yuanfeng.base.service.BaseArticleBaseService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.util.secverifyapiutil.Base64Utils;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseArticleBaseServiceImpl
 * @Description  网站初始化内容设置
 * @Author cyp
 * @Date 2023-06-07 15:17:52
 * @Version 1.0
 **/
@Service
public class BaseArticleBaseServiceImpl extends ServiceImpl<BaseArticleBaseMapper, BaseArticleBaseEntity>
        implements BaseArticleBaseService {


    @Override
    public ResponseResult queryArticleGroupDetail(Integer articleGroupId) {
        List<BaseArticleBaseEntity> articletitle = this.baseMapper.selectList(new LambdaQueryWrapper<BaseArticleBaseEntity>()
                             .eq(BaseArticleBaseEntity::getArticleGroupId,articleGroupId));
        return ResponseResult.success(articletitle);
    }

    @Override
    public ResponseResult articleQueryDetail(BaseArticleBaseDTO param) {
        BaseArticleBaseEntity baseArticleBaseEntity = this.baseMapper.selectById(param.getArticleId());
        return ResponseResult.success(baseArticleBaseEntity);
    }

    @Override
    public ResponseResult articleDelete(BaseArticleBaseDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            //删除
            this.baseMapper.deleteById(param.getArticleId());
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult articleUpdate(BaseArticleBaseDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            param.setArticleAddTime(new Date());
            String fromBase64 = null;
            try {
                String data = param.getArticleDesc();
                fromBase64 = Base64Utils.getFromBase64(data);

            } catch (Exception e) {
                e.printStackTrace();
                return ResponseResult.fail(BizCodeEnume.DECODING_FAILED.getCode(),BizCodeEnume.DECODING_FAILED.getMsg());
            }
            param.setArticleDesc(fromBase64);
            //存在就更新不存在就添加
            this.baseMapper.articleInsert(param);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(),BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
    }

    @Override
    public ResponseResult queryArticleBase(BaseArticleBaseDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        try {
            if (null != userInfo) {
                //将对象转换为map
                Map data = ParseMapUtils.beanToMap(param);
                //分页查询数据
                IPage<BaseArticleBaseEntity> page = this.baseMapper.queryArticleBase(new Query<BaseArticleBaseEntity>().getPage(data), param);

                return ResponseResult.success(PageUtils.getPage(page));
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(),BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
    }
}
